/**
 * Created by midori on 2015/12/7.
 */
(function(){
    var TestService, module,
        __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; };

    TestService = (function(){
        function TestService(Restangular, $q, RestAnaularWrapper, PromiseWrapper, eventBus){
            this.Restangular = Restangular;
            this.$q = $q;
            this.RestAngularWrapper = RestAnaularWrapper;
            this.PromiseWrapper = PromiseWrapper;
        }

        TestService.prototype._cachedTestSummaries = {};

        TestService.prototype.getTestSummary = function(testId) {
            if (this._cachedTestSummaries[testId] == null) {
                this._cacheTestSummary(testId, this._retrieveTestSummary(testId));
            }
            return this._cachedTestSummaries[testId];
        };

        TestService.prototype._cacheTestSummary = function(testId, testSummaryPromise){
            this._cachedTestSummaries[testId] = testSummaryPromise;
        };

        TestService.prototype._retrieveTestSummary = function(testId) {
            return this.RestAngularWrapper.make(this.Restangular.one("test-plan", testId).one("summary").get());
        };

        TestService.prototype.getTestReport = function(reportId) {
            return this.RestAngularWrapper.make(this.Restangular.one("report", reportId).get());
        };

        //TestService.prototype._cacheTestSummary = function(testId, testSummaryRW) {
        //    var testSummaryPW;
        //    testSummaryPW = this._cachedTestSummaries[testId] = this.PromiseWrapper.make(testSummaryRW, "testSummary '" + testId + "'");
        //    return testSummaryPW.then((function(_this) {
        //        return function(testSummary) {
        //            var testRunId, _ref;
        //            if (_this._projectToTestIdsMap[testSummary.projectId] == null) {
        //                _this._projectToTestIdsMap[testSummary.projectId] = {};
        //            }
        //            _this._projectToTestIdsMap[testSummary.projectId][testSummary.testId] = true;
        //            testRunId = (_ref = testSummary.data) != null ? _ref.optLastRunId : void 0;
        //            if (testRunId != null) {
        //                return _this._testRunIdToTestIdMap[testRunId] = testSummary.testId;
        //            }
        //        };
        //    })(this));
        //};
        TestService.prototype._cachedTestConfigurations = {};

        TestService.prototype.getTestConfiguration = function(testId){
            if (this._cachedTestConfigurations[testId] == null){
                this._cachedTestConfigurations[testId] = this._retrieveTestConfiguration(testId);
            }
            return this._cachedTestConfigurations[testId];
        };

        TestService.prototype._retrieveTestConfiguration = function(testId){
            return this.RestAngularWrapper.make(this.Restangular.one("test-plan",testId).one("config").get());
        };

        return TestService;

    })();

    module = angular.module("services.TestService", ["restangular", "services.utils.RestAngularWrapper", "services.PromiseWrapper"]);

    module.factory('TestService', ["Restangular", "$q", "RestAngularWrapper","PromiseWrapper"
        ,function(Restangular, $q, RestAngularWrapper, PromiseWrapper){
            return new TestService(Restangular, $q, RestAngularWrapper, PromiseWrapper);
        }]);

}).call(this);